<%@ page import="web.servlet.eljstl.User" %>
<%@ page import="java.util.*" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/6/11
  Time: 16:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>表格</title>
    <style>
        #tr_g {
            background-color: aquamarine;
        }

        #tr_b {
            background-color: blanchedalmond;
        }

        table {
            border: solid goldenrod 2px;
        }
    </style>
</head>
<body>
<h2>使用foreach将数据展示在表格</h2>
<%
    User user1 = new User("张三", 12, new Date());
    User user2 = new User("李斯", 34, new Date());
    User user3 = new User("王五", 45, new Date());
    User user4 = new User("李六", 23, new Date());
    List<User> userListlist = new ArrayList<User>();
    userListlist.add(user1);
    userListlist.add(user2);
    userListlist.add(user3);
    userListlist.add(user4);
    request.setAttribute("userList", userListlist);
    Map<Integer, User> userMap = new HashMap<Integer, User>();
    userMap.put(1, user1);
    userMap.put(2, user2);
    userMap.put(3, user3);
    userMap.put(4, user4);
    request.setAttribute("userMap", userMap);
%>
<table border="1" width="500" align="center">
    <tr>
        <%--表头--%>
        <th>姓名</th>
        <th>年龄</th>
        <th>日期</th>
        <th>生日</th>
    </tr>
    <%--items 对象容器:用El表达式取出域中的对象--%>
    <c:forEach items="${userList}" var="user" varStatus="s">
        <c:if test="${s.count%2 == 0}">
            <tr id="tr_b">
                <td>${s.count}</td>
                <td>${user.name}</td>
                <td>${user.age}</td>
                <td>${user.birthday}</td>

            </tr>
        </c:if>
        <c:if test="${s.count%2 == 1}">
            <tr id="tr_g">
                <td>${s.count}</td>
                <td>${user.name}</td>
                <td>${user.age}</td>
                <td>${user.birthday}</td>
            </tr>
        </c:if>
    </c:forEach>
</table>
<table border="1" width="400" align="center">
    <tr>
        <%--表头--%>
        <th>姓名</th>
        <th>年龄</th>
        <th>日期</th>
        <th>生日</th>
    </tr>
    <%--items 对象容器:用El表达式取出域中的对象--%>
    <c:forEach items="${userMap}" var="user" varStatus="s">
        <c:if test="${s.count%2 == 0}">
            <tr id="tr_b">
                <td>${s.count}</td>
                <td>${user.value.name}</td>
                <td>${user.value.age}</td>
                <td>${user.value.birthday}</td>

            </tr>
        </c:if>
        <c:if test="${s.count%2 == 1}">
            <tr id="tr_g">
                <td>${s.count}</td>
                <td>${user.value.name}</td>
                <td>${user.value.age}</td>
                <td>${user.value.birthday}</td>
            </tr>
        </c:if>
    </c:forEach>
</table>
</body>
</html>
